package com.ssm.icc.server.controller.estate;



import com.github.pagehelper.PageInfo;
import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.common.web.json.Tip;
import com.jee.ssm.common.web.param.ParamMap;
import com.ssm.icc.dao.model.estate.Estate;
import com.ssm.icc.rpc.api.estate.EstateService;
import com.ssm.upms.log.AdminControllerLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;

/**
 * 物业管理 Controller
 *
 * @author GaoXiang
 * @version 1.0
 */
@Controller
@RequestMapping("/estate")
public class EstateController {


    /**
     * 进入物业添加页面 携带一个生成的id --> longId
     *
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value = "/add")
    @RequiresPermissions("estate:add")
    public String add(ModelMap model) {
        model.put("longId", UUIDFactory.getStringId());
        return "manager/estate/add";
    }

    /**
     * 进入物业编辑页面
     *
     * @param model 返回物业的容器
     * @param id    物业id
     * @return 编辑页面
     */
    @RequestMapping(value = "/edit")
    @RequiresPermissions("estate:edit")
    public String edit(ModelMap model, @RequestParam String id) {
        model.put("data", estateService.selectById(id));
        return "manager/estate/edit";
    }


    /**
     * 物业添加
     *
     * @param estate 带id的物业对象
     * @return 成功状态
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    @RequiresPermissions("estate:add")
    @AdminControllerLog(description = "添加物业")
    public Tip save(Estate estate) {

        try {
            estateService.insert(estate);
            return new Tip();
        } catch (Exception e) {
            return new Tip(1, "添加失败！");
        }
    }


    /**
     * 根据 id 修改物业
     *
     * @param estate 带id的物业对象
     * @return 成功状态
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @RequiresPermissions("estate:edit")
    @AdminControllerLog(description = "修改物业")
    public Tip update(Estate estate) {

        try {
            estateService.updateById(estate);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1, "修改失败！");
        }

    }


/*
    */
/**
 * 根据 id 删除物业
 * @param id 物业id
 * @return 成功状态
 *//*

    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("estate:delete")
    @AdminControllerLog(description = "删除物业" )
    public Tip delete(@RequestParam String id) {

        try {

            List<Residence> rl = residenceService.listByEstateId(id);

            for(Residence r : rl) {
                residenceService.deleteById(r.getId());
                buildingService.deleteByResidenceId(r.getId());
                unitService.deleteByResidenceId(r.getId());
                roomService.deleteByResidenceId(r.getId());
                userRoomService.deleteByResidenceId(r.getId());
                cardService.deleteByResidenceId(r.getId());
                List<Equipment> equipmentList = equipmentService.selectByResidenceId(r.getId());//查询小区下的所有设备
                if(equipmentList.size() > 0){
                    for (Equipment equipment:equipmentList) {
                        equipmentCardService.deleteByEquipmentId(equipment.getId());//根据设备id 删除
                    }
                }
                equipmentService.deleteByResidenceId(r.getId());
            }

            estateService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }
*/


    /**
     * 根据 id 列表批量删除
     *
     * @param ids 物业id List
     * @return 成功状态
     */
    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    @RequiresPermissions("estate:delete")
    @AdminControllerLog(description = "批量删除物业")
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            estateService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1, "批量删除失败！");
        }

    }


    /**
     * 根据 id 查找物业
     *
     * @param id 物业id
     * @return 物业对象 json
     */
    @RequestMapping(value = "/findJson")
    @ResponseBody
    @RequiresPermissions("estate:list")
    public Estate find(@RequestParam String id) {

        return estateService.selectById(id);
    }


    /**
     * 获取物业列表 获取全部 不分页
     *
     * @param request 请求参数
     * @return 物业列表页面
     */
    @RequestMapping(value = "/all")
    @RequiresPermissions("estate:list")
    public String all(HttpServletRequest request, ModelMap modelMap) {

        return list(request, modelMap, 1, 0);
    }


    /**
     * 获取物业列表 分页
     *
     * @param request 请求参数
     * @param page    第几页
     * @param size    每页大小
     * @return 物业列表页面
     */
    @RequestMapping(value = "/list")
    @RequiresPermissions("estate:list")
    public String list(HttpServletRequest request, ModelMap modelMap, Integer page, Integer size) {
        PageInfo<Estate> p = estateService.list(new ParamMap(request), page, size);
        modelMap.put("pageInfo", p);
        modelMap.putAll(new ParamMap(request));
        return "manager/estate/list";
    }
/*

    @RequestMapping(value="/listAll")
    @RequiresPermissions("estate:list")
    @ResponseBody
    public List<Estate> listAll(){
        List<Estate> estate = estateService.checkList(null);
        return estate;
    }

    @RequestMapping(value="/checklist")
    public String chooseEstate(ModelMap modelMap, HttpSession session,HttpServletRequest request){
       Account account= (Account) session.getAttribute(Const.ACCOUNT);
       ParamMap paramMap=new ParamMap(request);
       if(account.getType()==2){
           paramMap.put("id",account.getInfoId());
       }
       List<Estate> list= estateService.checkList(paramMap);
      */
/* for (Estate estate:list){
           estate.setAccount(accountService.findByInfoId(estate.getId()));
       }*//*

       modelMap.put("list",list);
       return  "manager/estate/chooseEstate";
    }

    @RequestMapping(value="/sensitivePower")
    @ResponseBody
    public String sensitivePower(HttpSession session){
        Account account= (Account) session.getAttribute(Const.ACCOUNT);
        if(account.getType()==2){
            return estateService.selectById(account.getInfoId()).getSensitivePower();
        }else{
            return "1";
        }
    }

*/

 /*   *//**
     * 查询物业管理员列表
     * @return
     *//*
    @RequestMapping(value="/selectEstate")
    public String selectEstate( String estateId,ModelMap model){
        List<Account> accountList = accountService.selectEstateByInfoId(estateId);
        for(Account a : accountList){
            a.setEstate(estateService.selectById(a.getInfoId()));
        }
        model.put("accountList",accountList);
        model.put("estateId",estateId);
        return  "manager/estate/estateList";
    }
*/

    /**
     * 进入物业管理员添加页面，携带一个声称的id
     *
     * @param model 返回实体容器
     * @return 添加页面
     */
    @RequestMapping(value = "/addEstate")
    public String addEstate(ModelMap model, @RequestParam String estateId) {
        model.put("longId", UUIDFactory.getStringId());
        model.put("estateId", estateId);
        return "manager/estate/addEstate";
    }

/*    *//**
     * 物业管理员的保存
     * @param account 物业对象
     * @return 成功状态
     *//*
    @RequestMapping(value = "/saveEstate")
    @ResponseBody
    public Tip saveEstate(Account account){
        try {
             account.setId(UUIDFactory.getStringId());
             account.setPassword(MD5.md5(account.getPassword()));
             account.setType(2);
             account.setInfoId( account.getEstate().getId());
             accountService.insert(account);
             AccountRole accountRole=new AccountRole();
             accountRole.setId(UUIDFactory.getStringId());
             accountRole.setAccountId(account.getId());
             accountRole.setRoleId("5074784684692686358");
             accountRoleService.insert(accountRole);
             return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"添加失败！");
        }
    }


    *//**
     *根据id删除信息
     * @param id
     * @return
     *//*
    @RequestMapping(value="/deleteEstate")
    @ResponseBody
    public Tip deleteResAdmin(@RequestParam String id){
        try {
            accountRoleService.deleteByAccountId(id);
            accountService.deleteById(id);
            return  new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"删除失败");
        }
    }


    *//**
     * 访问报警页面
     * @return 页面
     *//*
    @RequestMapping(value = "/addPointMes")
    @RequiresPermissions("estate:addPointMes")
    public String addPointMes(HttpSession session,ModelMap modelMap){
        Account account= (Account) session.getAttribute(Const.ACCOUNT);
        if(account.getType()==1 || account.getType()==2 ){
         modelMap.put("data", settingsService.selecByAccountId(account.getInfoId()));
        }else if(account.getType()==21){
            modelMap.put("data",settingsService.selecByAccountId(account.getInfoId().split(",")[0]));
        }
        return "manager/estate/addPointMes";
    }



    *//**
     * 修改报警页面的信息
     * @param settings
     * @return 成功状态
     *//*
    @RequestMapping(value = "/editErrorMes")
    @ResponseBody
    public Tip editErrorMes(Settings settings,HttpSession session,ModelMap modelMap){
        try {
            Account account= (Account) session.getAttribute(Const.ACCOUNT);
            Settings s=null;
            if(account.getType()==2){
                s= settingsService.selecByAccountId(account.getInfoId());
            }else if(account.getType()==21){
                s= settingsService.selecByAccountId(account.getInfoId().split(",")[0]);
            }else if(account.getType()==1){
                System.out.println(settings.getAccountId());
                s= settingsService.selecByAccountId(settings.getAccountId());
            }
            if(s==null){
                settings.setId(UUIDFactory.getStringId());
                settings.setName("设置长时间未刷卡的警报");
                if(account.getType()==2 || account.getType()==1){
                    settings.setAccountId(account.getInfoId());
                } else if(account.getType()==21){
                    settings.setAccountId(account.getInfoId().split(",")[0]);
                }
                settingsService.insert(settings);
            }else{
                settingsService.updateById(settings);
            }
            return  new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return  new Tip(1,"修改失败");
        }

}*/



    //---------------------------- property -------------------------------

    @Resource
    private EstateService estateService;

    /*@Resource
    AccountService accountService;

    @Resource
    private AccountRoleService accountRoleService;

    @Resource
    private ResidenceService residenceService;

    @Resource
    private BuildingService buildingService;

    @Resource
    private UnitService unitService;

    @Resource
    private RoomService roomService;

    @Resource
    private UserRoomService userRoomService;

    @Resource
    private EquipmentService equipmentService;

    @Resource
    private CardService cardService;

    @Resource
    private EquipmentCardService equipmentCardService;

    @Resource
    private SettingsService settingsService;

    @Resource

    private NewsService newsService;
*/
}
